package algorithmTopics.devise;

import java.util.ArrayList;

public class MinStack {

    /**
     * Your MinStack object will be instantiated and called as such:
     * MinStack obj = new MinStack();
     * obj.push(val);
     * obj.pop();
     * int param_3 = obj.top();
     * int param_4 = obj.getMin();
     */
    private int val;
    private MinStack next;
    private ArrayList<Integer> min;
    public MinStack() {

    }
    {
        ArrayList<Integer> integers = new ArrayList<>();
        this.min = integers;
    }

    public void push(int val) {
        this.min.add(val);
        MinStack minStack = new MinStack();
        minStack.val = val;
        if (this.next != null) {
            minStack.next = this.next;
        }
        this.next = minStack;

    }

    public void pop() {
        this.min.remove(this.min.indexOf(this.next.val));
        this.next = next.next;
    }

    public int top() {
        return this.next.val;
    }

    public int getMin() {
        int min = this.min.get(0);
        for (int i = 1; i < this.min.size(); i++) {
            if (min > this.min.get(i)) {
                min = this.min.get(i);
            }
        }
        return min;
    }

}
